package com.hzye.framework.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public final class ConvertUtil {

	/***************************************************************************
	 * 将对象转成整型
	 * 
	 * @param obj
	 * @return
	 */
	public static final int convertToInteger(Object obj) {
		if (StringUtil.isEmpty(obj)) {
			return 0;
		} else {
			try {
				return Integer.parseInt(obj.toString());
			} catch (NumberFormatException e) {
				e.equals(obj);
				return 0;
			}
		}
	}

	/***************************************************************************
	 * 将对象转成长整型
	 * 
	 * @param obj
	 * @return
	 */
	public static final long convertToLong(Object obj) {
		if (StringUtil.isEmpty(obj)) {
			return 0;
		} else {
			try {
				return Long.parseLong(obj.toString());
			} catch (NumberFormatException e) {
				return 0l;
			}
		}
	}

	/***************************************************************************
	 * 将数据转换成字符串,一般是处理in里面的语句
	 * 
	 * @param obj
	 * @return
	 */
	public static final String convertToStringByArray(String[] obj) {
		StringBuilder sb = new StringBuilder();
		if (obj == null || obj.length == 0) {
			return "";
		} else {
			sb.append("'-1'");
			for (int index = 0; index < obj.length; index++) {
				sb.append(",'").append(StringUtil.trim(obj[index])).append("'");
			}
			return sb.toString();
		}
	}
	/***************************************************************************
	 * 将数据转换成字符串,一般是处理in里面的语句
	 * 
	 * @param obj
	 * @return
	 */
	public static final String convertToIntByStringArray(String[] obj) {
		StringBuilder sb = new StringBuilder();
		if (obj == null || obj.length == 0) {
			return "";
		} else {
			sb.append("-1");
			for (int index = 0; index < obj.length; index++) {
				sb.append(",").append(obj[index]);
			}
			return sb.toString();
		}
	}

	/***
	 *　将Properties转换成Map
	 * @param p
	 * @return
	 * Map<String,String>
	 * Jun 17, 2012 1:00:23 PM
	 */
	public static Map<String, String> convertToMapFromProperties(Properties p) {
		Map<String, String> map = new HashMap<String, String>();
		Set<Object> set = p.keySet();
		for (Object o : set) {
			map.put(o.toString(), p.getProperty(o.toString()));
		}
		return map;

	}

}
